# 大狗AI助手 - PowerShell 快速配置脚本

Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "        🐕 大狗AI助手 - 快速配置" -ForegroundColor Yellow
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# 检查Node.js
try {
    $nodeVersion = node --version
    Write-Host "✅ Node.js 已安装: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ 未检测到Node.js，请先安装Node.js" -ForegroundColor Red
    Write-Host "📦 下载地址: https://nodejs.org/" -ForegroundColor Blue
    Read-Host "按回车键退出"
    exit 1
}

Write-Host ""

# 检查项目构建
if (-not (Test-Path "bundle\dagou.js")) {
    Write-Host "🔧 正在构建项目..." -ForegroundColor Yellow
    npm run build
    npm run bundle
    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ 构建失败，请检查错误信息" -ForegroundColor Red
        Read-Host "按回车键退出"
        exit 1
    }
    Write-Host "✅ 项目构建完成" -ForegroundColor Green
} else {
    Write-Host "✅ 项目已构建" -ForegroundColor Green
}

Write-Host ""

# 配置API密钥
if (-not $env:DEEPSEEK_API_KEY) {
    Write-Host "🔑 请输入你的DeepSeek API密钥:" -ForegroundColor Yellow
    Write-Host "💡 获取地址: https://platform.deepseek.com/api_keys" -ForegroundColor Blue
    $apiKey = Read-Host "API密钥"
    
    if (-not $apiKey) {
        Write-Host "❌ API密钥不能为空" -ForegroundColor Red
        Read-Host "按回车键退出"
        exit 1
    }
    
    # 设置环境变量
    [Environment]::SetEnvironmentVariable("DEEPSEEK_API_KEY", $apiKey, "User")
    $env:DEEPSEEK_API_KEY = $apiKey
    Write-Host "✅ API密钥已配置" -ForegroundColor Green
} else {
    Write-Host "✅ API密钥已存在" -ForegroundColor Green
}

Write-Host ""

# 创建全局命令
$batchPath = "C:\Windows\System32\dagou.bat"
$currentPath = Get-Location

try {
    "@echo off" | Out-File -FilePath $batchPath -Encoding ASCII
    "node `"$currentPath\bundle\dagou.js`" %*" | Out-File -FilePath $batchPath -Append -Encoding ASCII
    Write-Host "✅ 全局命令已创建" -ForegroundColor Green
} catch {
    Write-Host "❌ 创建全局命令失败，请以管理员身份运行PowerShell" -ForegroundColor Red
}

Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "             🎉 配置完成！" -ForegroundColor Yellow
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "现在你可以使用以下命令：" -ForegroundColor White
Write-Host ""
Write-Host "  dagou --help           查看帮助" -ForegroundColor Green
Write-Host "  dagou -p `"你好`"         快速提问" -ForegroundColor Green
Write-Host "  dagou                  进入交互模式" -ForegroundColor Green
Write-Host ""
Write-Host "示例：" -ForegroundColor White
Write-Host "  dagou -p `"写一个Python程序`"" -ForegroundColor Cyan
Write-Host "  dagou -m deepseek-coder -p `"创建一个网站`"" -ForegroundColor Cyan
Write-Host ""

# 测试安装
Write-Host "🧪 测试安装..." -ForegroundColor Yellow
try {
    dagou --help | Out-Null
    Write-Host "✅ 安装测试成功！" -ForegroundColor Green
} catch {
    Write-Host "❌ 测试失败，请重启命令行后再试" -ForegroundColor Red
}

Write-Host ""
Read-Host "按回车键退出"